home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / QuickDraw / PaletteAnimation gray / PaletteAnimation.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  4.3 KB  |  159 lines  |  [TEXT/MPS ]

  1. /*______________________________________________________*/
  2. /*                       Palette Demo                        */
  3. /*                          by                          */
  4. /*                  RICHARD P. COLLYER                  */
  5. /*              Developer Technical Support             */
  6. /*                 Apple Computer, Inc.                 */
  7. /*                       08/15/90                       */
  8. /*______________________________________________________*/
  9.  
  10. #include    <Quickdraw.h>
  11. #include    <Windows.h>
  12. #include    <Events.h>
  13. #include     <GestaltEqu.h>
  14. #include    <OSEvents.h>
  15. #include    <Palette.h>
  16. #include    <SegLoad.h>
  17. #include    <Packages.h>
  18.  
  19. extern _DataInit();
  20.  
  21. #define    TRUE            0xFF
  22. #define    FALSE            0
  23. #define Gestalttest        0xA1AD
  24. #define NoTrap            0xA89F
  25.  
  26. #define    clutID            40
  27. #define    numcolor        256
  28.  
  29. Rect                WinMinusScroll;
  30. WindowPtr            myWindow;
  31. CTabHandle            mycolors;
  32. PaletteHandle        srcPalette;
  33. Boolean                DoneFlag;
  34.  
  35. /*______________________________________________________*/
  36. /*             Set Up Usage of Palette                  */
  37. /*______________________________________________________*/
  38. void Draw()
  39. /* The drawing routine is a collection of 253 random Rects which are colored with 
  40. the corresponding color in the color table. */
  41. {
  42.     float                    left, top, right, bottom;
  43.     Rect                    CRect;
  44.     int                        i;
  45.  
  46.     EraseRect (&WinMinusScroll);
  47.     for (i = 1; i < numcolor-2; ++i) {
  48.         do {
  49.             left = Random() / 32767.0 * WinMinusScroll.right;
  50.             if (left < 0)
  51.                 left = -left;
  52.             } while (left > WinMinusScroll.right - 40);
  53.             
  54.         do {
  55.             right = Random() / 32767.0 * WinMinusScroll.right;
  56.             if (right < 0)
  57.                 right = -right;
  58.             } while (right < left);
  59.             
  60.         do {
  61.             top = Random() / 32767.0 * WinMinusScroll.bottom;
  62.             if (top < 0)
  63.                 top = -top;
  64.             } while (top > WinMinusScroll.bottom - 40);
  65.             
  66.         do {
  67.             bottom = Random() / 32767.0 * WinMinusScroll.bottom;
  68.             if (bottom < 0)
  69.                 bottom = -bottom;
  70.             } while (bottom < top);
  71.         SetRect(&CRect, left, top, right, bottom);
  72.         PmForeColor(i);
  73.         FrameRect (&CRect);
  74.         PaintRect (&CRect);
  75.         }
  76. }
  77.  
  78. /*______________________________________________________*/
  79. /*               Initialization traps                   */
  80. /*______________________________________________________*/
  81. void init()
  82. {
  83.     Rect                BaseRect;
  84.     OSErr                err;
  85.     long                feature;
  86.     
  87.     UnloadSeg(_DataInit);
  88.     InitGraf(&qd.thePort);
  89.     FlushEvents(everyEvent, 0);
  90.     InitWindows();
  91.     InitCursor();
  92.     
  93.     DoneFlag = FALSE;
  94. /*______________________________________________________*/
  95. /*            Use Gestalt to find 32BQD                 */
  96. /*______________________________________________________*/
  97. if ((GetTrapAddress(Gestalttest) != GetTrapAddress(NoTrap))) {
  98.     err = Gestalt(gestaltQuickdrawVersion, &feature);
  99.     if (!err) {
  100.         if ((feature & 0x0f00) != 0x0200)
  101.             DoneFlag = TRUE;
  102.         }
  103.     else 
  104.         DoneFlag = TRUE;
  105.     }
  106. else
  107.     DoneFlag = TRUE;
  108. /*______________________________________________________*/
  109. /*                     Set Rects                        */
  110. /*______________________________________________________*/
  111.     SetRect(&BaseRect, 40, 60, 472, 282);
  112.     SetRect(&WinMinusScroll, BaseRect.left-40, BaseRect.top-60, BaseRect.right-60, 
  113.                 BaseRect.bottom - 80);
  114. /*______________________________________________________*/
  115. /*        Open Window & set Palette & Picture           */
  116. /*______________________________________________________*/
  117.     mycolors = GetCTable (clutID);
  118.  
  119.     myWindow = NewCWindow(nil, &BaseRect, "\pUsing Palette Manager", TRUE, documentProc, 
  120.                             (WindowPtr) -1, TRUE, 150);
  121.     SetPort((WindowPtr) myWindow);
  122.  
  123.     srcPalette = NewPalette (numcolor, mycolors, pmAnimated+pmExplicit, 0);
  124.     SetPalette ((WindowPtr) myWindow, srcPalette, TRUE);
  125.     
  126.     Draw();
  127. }
  128.  
  129. main()
  130. {
  131.     EventRecord     myEvent;
  132.     int                yieldTime = 0;
  133.     RGBColor        changecolor;
  134. /*______________________________________________________*/
  135. /*                   Main Event loop                    */
  136. /*______________________________________________________*/
  137.     init();
  138.     for ( ;; ) {
  139.         if (DoneFlag)
  140.             ExitToShell();
  141.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  142.             switch (myEvent.what) {
  143.                 case mouseDown:
  144.                 case keyDown:
  145.                 case autoKey:
  146.                     DoneFlag = TRUE;
  147.                     break;
  148.                 default:
  149.                     break;
  150.                 }
  151.             }
  152.             
  153.         /* Animate the colors one step for eash event loop */
  154.         GetEntryColor (srcPalette, 1, &changecolor);
  155.         AnimatePalette (myWindow, mycolors, 2, 1, numcolor - 2);
  156.         AnimateEntry (myWindow, numcolor - 1, &changecolor);
  157.         Palette2CTab (srcPalette, mycolors);
  158.         }
  159. }